package jaux.tank.admin.config.properties;

import java.io.File;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import jaux.tank.utils.ToolUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * tank项目配置
 *
 * @Date 2017/5/23 22:31
 */
@Component
@ConfigurationProperties(prefix = TankProperties.PREFIX)
@Getter @Setter
public class TankProperties {

    public static final String PREFIX = "tank";
    
    public static final String LOGIN_VIEW = "/login.html";

    private Boolean kaptchaOpen = false;

    private Boolean swaggerOpen = false;

    private String fileUploadPath;
    
    private boolean localLogin = true;
    
    private boolean validLoginId = false;
    
    private boolean loginPasswdEncrypted = false;

    private Boolean haveCreatePath = false;
    
    private Boolean sessionOpen = false;
    
    private Boolean mutiDatasourceOpen = false;

    private Boolean springSessionOpen = false;

    private Integer sessionInvalidateTime = 30 * 60;  //session 失效时间（默认为30分钟 单位：秒）

    private Integer sessionValidationInterval = 15 * 60;  //session 验证失效时间（默认为15分钟 单位：秒）
    
    private String baseDir = "/tmp/jaux/tank/data";
    
    private String workAttachmentSavedir = "/tmp/jaux/tank/data/workattachment";
    
    private Long workAttachmentLimitToDb = 1048576l;

    private boolean workAttachmentSaveToS3 = false;
    
    private String[] urlWithAuth=new String[] {"/api/attachment/download"};
    
    /**
	 * 可以匿名访问的路径
	 */
	private String[] urlAnonymous = new String[] {};
    
    public String getFileUploadPath() {
        //如果没有写文件上传路径,保存到临时目录
        if (ToolUtil.isEmpty(fileUploadPath)) {
        	String tempPath =ToolUtil.getTempPath();
        	if(!tempPath.endsWith(File.separator)) {
        		tempPath = tempPath + File.separator;
        	}
            return tempPath;
        } else {
            //判断有没有结尾符,没有得加上
            if (!fileUploadPath.endsWith(File.separator)) {
                fileUploadPath = fileUploadPath + File.separator;
            }
            //判断目录存不存在,不存在得加上
            if (Boolean.FALSE.equals(haveCreatePath) ) {
                File file = new File(fileUploadPath);
                file.mkdirs();
                haveCreatePath = true;
            }
            return fileUploadPath;
        }
    }

    public void setFileUploadPath(String fileUploadPath) {
        this.fileUploadPath = fileUploadPath;
    }

}
